package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.vo.NameListValue;
import com.ruoyi.system.domain.vo.NameValue;
import com.ruoyi.system.domain.vo.OpAuthorSensitiveDayNumVM;
import com.ruoyi.system.domain.vo.OpMediaNameSensitiveDayNumVM;
import com.ruoyi.system.service.IGatherTaskService;
import com.ruoyi.system.task.TimeConsumingTask;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PublicOpinionTopic;
import com.ruoyi.system.service.IPublicOpinionTopicService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 舆情主题Controller
 * 
 * @author ruoyi
 * @date 2021-07-14
 */
@RestController
@RequestMapping("/system/topic")
public class PublicOpinionTopicController extends BaseController
{
    @Autowired
    private IPublicOpinionTopicService PublicOpinionTopicService;
    @Autowired
    private IGatherTaskService gatherTaskService;


    /**
     * 舆情数量按省分组
     */
    @ApiOperation("舆情数量按省分组")
    @GetMapping("/getPublicOpinionCountGroupByProvinceOfTopicId")
    public AjaxResult getPublicOpinionCountGroupByProvinceOfTopicId(Long topicId) {
        List<NameValue> list = PublicOpinionTopicService.getPublicOpinionCountGroupByProvinceOfTopicId(topicId);
        return AjaxResult.success(list);
    }


    /**
     * 互动声量
     */
    @ApiOperation("互动声量")
    @GetMapping(value = "/getInteractiveInformation")
    public AjaxResult getInteractiveInformation(Long topicId) {
        return AjaxResult.success(PublicOpinionTopicService.getInteractiveInformation(topicId));
    }

    /**
     * 影响力值
     */
    @ApiOperation("影响力值")
    @GetMapping(value = "/getInfluenceValue")
    public AjaxResult getInfluenceValue(Long topicId) {
        return AjaxResult.success(PublicOpinionTopicService.getInfluenceValue(topicId));
    }

    /**
     * 信息总览
     */
    @ApiOperation("信息总览")
    @GetMapping(value = "/getAllInformation")
    public AjaxResult getAllInformation(Long topicId) {
        return AjaxResult.success(PublicOpinionTopicService.getAllInformation(topicId));
    }



    /**
     * 查询舆情主题列表
     */
    @PreAuthorize("@ss.hasPermi('topic:list')")
    @GetMapping("/list")
    public TableDataInfo list(PublicOpinionTopic publicOpinionTopic)
    {
        startPage();
        publicOpinionTopic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> list = PublicOpinionTopicService.selectPublicOpinionTopicList(publicOpinionTopic);
        return getDataTable(list);
    }


    /**
     * 查询舆情主题列表
     */
    @GetMapping("/listForAnonymous")
    public TableDataInfo listForAnonymous(PublicOpinionTopic publicOpinionTopic)
    {
        startPage();
        List<PublicOpinionTopic> list = PublicOpinionTopicService.selectPublicOpinionTopicList(publicOpinionTopic);
        return getDataTable(list);
    }

    /**
     * 导出舆情主题列表
     */
    @PreAuthorize("@ss.hasPermi('topic:export')")
    @Log(title = "舆情主题", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PublicOpinionTopic publicOpinionTopic)
    {
        publicOpinionTopic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> list = PublicOpinionTopicService.selectPublicOpinionTopicList(publicOpinionTopic);
        ExcelUtil<PublicOpinionTopic> util = new ExcelUtil<>(PublicOpinionTopic.class);
        return util.exportExcel(list, "舆情主题数据");
    }

    /**
     * 获取舆情主题详细信息
     */
    @PreAuthorize("@ss.hasPermi('topic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(PublicOpinionTopicService.selectPublicOpinionTopicById(id));
    }

    /**
     * 新增舆情主题
     */
    @PreAuthorize("@ss.hasPermi('topic:add')")
    @Log(title = "舆情主题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PublicOpinionTopic publicOpinionTopic)
    {
        publicOpinionTopic.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        publicOpinionTopic.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        publicOpinionTopic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        PublicOpinionTopicService.insertPublicOpinionTopic(publicOpinionTopic);
        //创建采集任务
        gatherTaskService.batchCreateGatherTask(publicOpinionTopic);
        // 创建一个耗时任务对象
        TimeConsumingTask task = new TimeConsumingTask(null,publicOpinionTopic.getId());

        // 创建一个线程对象，并将耗时任务对象作为参数传入
        Thread thread = new Thread(task);

        // 开启线程
        thread.start();
        return toAjax(true);
    }

    /**
     * 修改舆情主题
     */
    @PreAuthorize("@ss.hasPermi('topic:edit')")
    @Log(title = "舆情主题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PublicOpinionTopic publicOpinionTopic)
    {
        int res=PublicOpinionTopicService.updatePublicOpinionTopic(publicOpinionTopic);

        //重新创建采集任务
        gatherTaskService.batchUpdateGatherTask(PublicOpinionTopicService.selectPublicOpinionTopicById(publicOpinionTopic.getId()));
        // 创建一个耗时任务对象
        TimeConsumingTask task = new TimeConsumingTask(null,publicOpinionTopic.getId());

        // 创建一个线程对象，并将耗时任务对象作为参数传入
        Thread thread = new Thread(task);

        // 开启线程
        thread.start();
        return toAjax(res);
    }

    /**
     * 删除舆情主题
     */
    @PreAuthorize("@ss.hasPermi('topic:remove')")
    @Log(title = "舆情主题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int res=PublicOpinionTopicService.deletePublicOpinionTopicByIds(ids);
        //删除采集任务
        gatherTaskService.batchDeleteGatherTask("topic",ids);
        return toAjax(res);
    }


    /**
     * 获取正负面信息趋势图
     */
    @ApiOperation(value = "获取正负面信息趋势图", notes = "获取正负面信息趋势图")
    @GetMapping("/getSentimentByPublishTimeByTopicId")
    public TableDataInfo getSentimentByPublishTimeByTopicId(Long topicId) {
        List<NameListValue> list = PublicOpinionTopicService.getSentimentByPublishTimeByTopicId(topicId);
        return getDataTable(list);
    }
    /**
     * 信息载体趋势图
     */
    @ApiOperation(value = "信息载体趋势图", notes = "信息载体趋势图")
    @GetMapping("/getMediaTypeByPublishTimeByTopicId")
    public TableDataInfo getMediaTypeByPublishTimeByTopicId(Long topicId) {
        List<NameListValue> list = PublicOpinionTopicService.getMediaTypeByPublishTimeByTopicId(topicId);
        return getDataTable(list);
    }



    /**
     * 获取主题热词
     */
    @ApiOperation(value = "获取主题热词", notes = "获取主题热词")
    @GetMapping("/getKeywordsByTopicId")
    public TableDataInfo getKeywordsByTopicId(Long topicId) {
        List<NameValue> list = PublicOpinionTopicService.getKeywordsByTopicId(topicId, 300);
        return getDataTable(list);
    }



    /**
     * 媒体活跃排行榜（情感）
     */
    @ApiOperation(value = "媒体活跃排行榜（情感）", notes = "媒体活跃排行榜（情感）")
    @GetMapping("/countSensitiveOpinionByMediaName")
    public TableDataInfo countSensitiveOpinionByMediaName(Long topicId) {
        List<OpMediaNameSensitiveDayNumVM> list = PublicOpinionTopicService.countSensitiveOpinionByMediaName(topicId);
        return getDataTable(list);
    }


    /**
     * 意见领袖排行（情感）
     */
    @ApiOperation(value = "意见领袖排行（情感）", notes = "意见领袖排行（情感）")
    @GetMapping("/countSensitiveAuthorOpinion")
    public TableDataInfo countSensitiveAuthorOpinion(Long topicId) {
        List<OpAuthorSensitiveDayNumVM> list = PublicOpinionTopicService.countSensitiveAuthorOpinion(topicId);
        return getDataTable(list);
    }






}
